﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using VoxelBlock.VoxelBlock3D;
using VoxelBlock;
using UnityEngine.SceneManagement;
public class MeshToVoxel : MonoBehaviour {


    public GameObject[] gameObjects;

    public GameObject gamo;

    public ParticleSystem part;
    public float startSize = 1;
    public float velocityPower = 100f;
    public float startLifetime = 1f;


    //[SerializeFiel
    //Vector2Int findSize = new Vector2Int(30, 70);

    [SerializeField]
    Camera[] selectCameras;

    [SerializeField]
    LayerMask layerMask;

    [SerializeField]
    Texture2D texture;

    //SimpleVoxel[] sVoxels = null;
    //Color[] overAllColor = null;
    void Update()
    {
        //if (list.Count == 0 && colors.Count == 0)
        if (Input.GetKeyDown(KeyCode.A))
        {
            GameObject gameObjectItem = new GameObject();

            for (int i = 0; i < gameObjects.Length; i++)
            {
                gameObjectItem.SetActive(false);
                gameObjectItem = gameObjects[i];
                gameObjectItem.SetActive(true);

                List<Voxel3D> list = new List<Voxel3D>();
                Dictionary<Color, int> colors = new Dictionary<Color, int>();
                //Vector3 cursorPos = Input.mousePosition;
                for (int x = 0; x < Screen.width; x++)
                {
                    for (int y = 0; y < Screen.height; y++)
                    {
                        foreach (var selectCamera in selectCameras)
                        {
                            Vector3 cursorPos = new Vector3(x, y, 0);
                            Ray cameraRay = selectCamera.ScreenPointToRay(cursorPos);
                            RaycastHit objhit;
                            if (Physics.Raycast(cameraRay, out objhit, 10000, layerMask))
                            {
                                Vector3Int point = objhit.point;
                                //Texture2D texture = (Texture2D)
                                texture = (Texture2D)objhit.transform.GetComponent<MeshRenderer>().material.mainTexture;
                                //objhit.textureCoord

                                int editingTexWidth = texture.width;
                                int editingTexHeight = texture.height;
                                Vector2 pixelUV = objhit.textureCoord;
                                int pixelUVx = (int)(pixelUV.x * editingTexWidth);
                                int pixelUVy = (int)(pixelUV.y * editingTexHeight);
                                Color color = texture.GetPixel(pixelUVx, pixelUVy);
                                var key = color;
                                if (colors.ContainsKey(key))
                                {
                                    list.Add(new Voxel3D(point, colors[key]));
                                }
                                else
                                {
                                    colors[key] = colors.Count;
                                    list.Add(new Voxel3D(point, colors[key]));
                                }
                            }
                        }
                    }
                }

                Color[] overAllColor = new Color[256];
                int colorIndex = 0;
                foreach (var item in colors)
                {
                    if (colorIndex > 255)
                    {
                        break;
                    }
                    overAllColor[colorIndex] = item.Key;
                    colorIndex++;
                }

                VoxelEditorMastermind.instance.EditingTex.SetPixels(overAllColor);
                VoxelEditorMastermind.instance.EditingTex.Apply();

                SimpleVoxel[] sVoxels = VoxelEditorMastermind.instance.AddVoxels(list);

                VoxelEditorMastermind.instance.SaveMainVoxel(@"C:\Users\Administrator\Desktop\Voxel D\" + gameObjectItem.name + ".vox");
                VoxelEditorMastermind.instance.DrawRemoveBlock(sVoxels);
            }



            //gamo.SetActive(false);
        }

        /*
        if (Input.GetKeyDown(KeyCode.B))
        {

            //if (sVoxels != null && overAllColor != null)
            {
                Debug.Log(sVoxels.Length);
                VoxelEditorMastermind.instance.HideNowDrawVoxel3DCore();
                foreach (var vox in sVoxels)
                {
                    //part.Emit(new ParticleSystem.EmitParams()
                    //{
                    //    position = new Vector3(0, 1.0f, 0),
                    //    velocity = new Vector3(UnityEngine.Random.Range(-10f, 10), UnityEngine.Random.Range(-10f, 10), UnityEngine.Random.Range(-10f, 10)),
                    //    startSize = 0.1f,
                    //    startLifetime = 5,
                    //    startColor = new Color32((byte)UnityEngine.Random.Range(0, 255), (byte)UnityEngine.Random.Range(0, 255), (byte)UnityEngine.Random.Range(0, 255), 255)
                    //}, 1);
                    part.Emit(new ParticleSystem.EmitParams()
                    {
                        position = vox.position,
                        velocity = new Vector3(UnityEngine.Random.Range(-velocityPower, velocityPower),
                        UnityEngine.Random.Range(-velocityPower, velocityPower),
                        UnityEngine.Random.Range(-velocityPower, velocityPower)),

                        startSize = this.startSize,
                        startLifetime = this.startLifetime,
                        startColor = overAllColor[vox.uvKey]
                    }, 1);
                }
            }
        }
        */



    }

}
